package admin_service

import (
	"fmt"
	"shop/common/log"
	"shop/config/constant"
	"shop/dao"
	"shop/entity"
	"shop/service"
	"shop/utils/convertutil"
	"shop/utils/result"
)

func GetShopConfig(shopId int) *result.Result {
	return result.Ok(service.GetShopInfo(shopId))
}

func UpdateShopConfig(shopId int, updater *entity.ShopFilter) *result.Result {
	res := GetShopConfig(shopId)
	if res.Code != result.ErrCodeOk {
		return res
	}

	log.Infof("updateShopConfig, updater:%s", convertutil.ToJsonString(updater))
	oldInfo := res.Data.(*entity.ShopInfo)
	if updater.Name != nil && *updater.Name != "" {
		oldInfo.Name = *updater.Name
	}
	if updater.TakeOutEnable != nil {
		oldInfo.TakeOutEnable = *updater.TakeOutEnable
	}
	if updater.TakeInEnable != nil {
		oldInfo.TakeInEnable = *updater.TakeInEnable
	}
	if updater.PackingPrice != nil {
		oldInfo.PackingPrice = *updater.PackingPrice
	}
	if updater.SendingPrice != nil {
		oldInfo.SendingPrice = *updater.SendingPrice
	}
	if updater.SendingNeedPrice != nil {
		oldInfo.SendingNeedPrice = *updater.SendingNeedPrice
	}
	if updater.StartTime != nil {
		oldInfo.StartTime = *updater.StartTime
	}
	if updater.EndTime != nil {
		oldInfo.EndTime = *updater.EndTime
	}
	if updater.FlagShowShopPopularGoods != nil {
		oldInfo.FlagShowShopPopularGoods = *updater.FlagShowShopPopularGoods
	}
	if updater.ShopPopularGoodsMaxNum != nil {
		oldInfo.ShopPopularGoodsMaxNum = *updater.ShopPopularGoodsMaxNum
	}
	if updater.RelationPhones != nil {
		oldInfo.RelationPhones = *updater.RelationPhones
	}
	if updater.RelationQQ != nil {
		oldInfo.RelationQQ = *updater.RelationQQ
	}
	if updater.RelationWeixin != nil {
		oldInfo.RelationWeixin = *updater.RelationWeixin
	}
	if updater.Status != nil {
		oldInfo.Status = *updater.Status
	}
	if updater.Notice != nil {
		oldInfo.Notice = *updater.Notice
	}
	if updater.Address != nil {
		oldInfo.Address = *updater.Address
	}
	if updater.Description != nil {
		oldInfo.Description = *updater.Description
	}

	err := dao.GetRedisCli().HSet(constant.CacheKeyShopInfoMap, fmt.Sprintf("%d", shopId), convertutil.ToJsonString(oldInfo)).Err()
	if err != nil {
		return result.Fail(fmt.Sprintf("redis, shopInfo 更新失败, err: %v", err))
	}
	return result.Ok(nil)
}
